package com.yiaohuer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiaohuer.VO.UserVO;
import com.yiaohuer.entity.User;
import com.yiaohuer.dao.UserDao;
import com.yiaohuer.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-10-01 14:44:52
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public UserVO login(User user) {
        User userResult = userDao.selectOne(new QueryWrapper<User>().eq("user_id", user.getUserId()));
        if (userResult == null){
            return new UserVO();
        }
        if (userResult.getPassword().equals(user.getPassword())) {
            userResult.setPassword("******");
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userResult, userVO);
            return userVO;
        }
        return new UserVO();
    }
}
